package cn.edu.nuc.film.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.nuc.film.common.PageControl;
import cn.edu.nuc.film.common.Pager;
import cn.edu.nuc.film.common.Parameter;
import cn.edu.nuc.film.common.UnitHelper;
import cn.edu.nuc.film.dao.FilmCityMapper;
import cn.edu.nuc.film.dao.FilmMapper;
import cn.edu.nuc.film.entity.Film;
import cn.edu.nuc.film.entity.FilmCity;
import cn.edu.nuc.film.service.interfaces.FilmCityService;

@Service
public class FilmCityServiceImpl implements FilmCityService {
	
	@Autowired
	private FilmCityMapper fcMapper;
	@Autowired
	private FilmMapper filmMapper;
	/**
	 *  影城所属影片
	 */
	public Pager<Film> fcFilmList( PageControl pc,String name,int fcId){
		/**
		 * 设置dao层的参数列表( @Param("start") int start,
		 * @Param("offset") int offset,@Param("filmName") String filmName )
		 */
		Parameter para = new Parameter();
		para.setStart( (pc.getCurrentindex() - 1) * pc.getPagesize());
		para.setOffset( pc.getPagesize());
		para.setName(name);
		Integer fcid = 0;
		if( fcId != 0){
			fcid = fcId;
		}
		//获取每页的具体数据
		List<Film> list = filmMapper.filmList( para,fcid );
		
		//设置总记录数
		int rscount = filmMapper.count();
		//int rscount = list.size();
		/*if( name != null ){
			rscount = list.size();
		}else{
			rscount = filmMapper.count();
		}*/
		Pager<Film> pager = UnitHelper.execlist(rscount, list, pc);
		return pager;
		
	}
	
	
	
	/**
	 * 获取影城列表
	 */
	public Pager<FilmCity> list(PageControl pc, String name) {
		/**
		 * 设置dao层的参数列表( @Param("start") int start, @Param("offset") int
		 * offset,@Param("name") String name )
		 */
		Parameter para = new Parameter();
		para.setStart((pc.getCurrentindex() - 1) * pc.getPagesize());
		para.setOffset(pc.getPagesize());
		para.setName(name);
		// 获取每页的具体数据
		List<FilmCity> list = fcMapper.fclist(para);
		// 设置总记录数
		int pagecount = 0;
		if (name != null) {
			pagecount = list.size();
		} else {
			pagecount = fcMapper.count();
		}
		// 测试是否获得
		/*
		 * System.out.println("-----------------filmlist------------------");
		 * System.out.println(list);
		 */
		Pager<FilmCity> pager = UnitHelper.execlist(pagecount, list, pc);
		return pager;
	}
	
	/**
	 * 通过fcid来获取影城信息
	 */
	public FilmCity detail(int fcid) {
		FilmCity fc = fcMapper.selectByPrimaryKey(fcid);
		return fc;
	}
	/**
	 * 新增影城
	 */
	public int insert(FilmCity fc) {
		int count = fcMapper.insert(fc);
		if( count > 0){
			return count;
		}else{
			throw new RuntimeException("添加影城失败");
		}
		
	}
	/**
	 * 删除影城
	 */
	public int deleteFc(int fcid) {
		int count = fcMapper.deleteByPrimaryKey(fcid);
		return count;
	}
	/**
	 * 修改影城信息(这里只要修改影城名)
	 */
	public int updateFc( FilmCity fc) {
		int count = fcMapper.updateByPrimaryKeySelective(fc);
		return count;
	}

}
